package linuxbox.resource.servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import linuxbox.bean.*;
import linuxbox.BoxStatic;
import linuxbox.common.*;
import linuxbox.resource.logic.*;

public class BoxResourceCatAction extends Action
{
  protected static String editJSP = null;
  protected static String viewJSP = null;
  protected static String listJSP = null;

  BoxResourceCat home = new BoxResourceCat();

  public void doAction(HttpSession aSession, BoxSession aBoxSession,
  HttpServletRequest req, HttpServletResponse resp) throws Exception
  {
    String str = (String) req.getParameter("SORTBY");
    if ( str != null && str.length() > 0 )
       aSession.setAttribute("resourceCat.SORTBY", str);

    str = (String) req.getParameter("ASC");
    if ( str != null && str.length() > 0 )
       aSession.setAttribute("resourceCat.ASC", str);

    str = req.getParameter("SubmitCancel");
    if ( str != null )
    {
      sendFile(listJSP,req,resp);
      return;
    }

    int curEntryID = 0;
    str = req.getParameter("ID");
    if ( str != null)
    {
      curEntryID = Integer.parseInt(str);
    }
    str = (String) req.getParameter("SubmitSave");
    if ( str != null )
    {
      saveToBean(curEntryID, req);
      sendFile(listJSP,req,resp);
      return;
    }
    str = req.getParameter("SubmitUpdate");
    if ( str != null )
    {
      saveToBean(curEntryID, req);
      sendFile(listJSP,req,resp);
      return;
    }
    str = req.getParameter("SubmitDelete");
    if ( str != null )
    {
      if (aBoxSession.isResourceAdmin())
      {
        deleteBean(curEntryID);
        sendFile(listJSP,req,resp);
      }
      else
      {
        //TODOXX: send to permission not allowed page!
      }
      return;
    }
    str = req.getParameter("SubmitEdit");
    if ( str != null )
    {
      if (aBoxSession.isResourceAdmin())
      {
        // Now we'll go to the edit form
        if ( curEntryID == 0 )
        {
          setDefaultAttributes(req);
        }
        else
        {
          setBeanAttributes(req, curEntryID);
        }
        sendFile(editJSP,req,resp);
        return;
      }
      else
      {
        // Not admin, can only view it
        if ( curEntryID != 0 )
        {
          setBeanAttributes(req, curEntryID);
          sendFile(viewJSP,req,resp);
          return;
        }
      }
    }
    str = req.getParameter("SubmitView");
    if ( str != null && curEntryID != 0 )
    {
      setBeanAttributes(req, curEntryID);
      sendFile(viewJSP,req,resp);
      return;
    }
    sendFile(listJSP,req,resp);
  }

  protected void saveToBean(int id, HttpServletRequest req)
  {
    BoxResourceCatEntry entry = null;
    if ( id == 0 )
      entry = home.add();
    else
      entry = home.get(new BaseIntKey(id));

    String str = req.getParameter("NAME");
    if ( str != null && str.length()>0)
      entry.setName(str);
    else
      entry.setName("No Title");

    str = req.getParameter("DESCRIPTION");
    if ( str != null )
      entry.setDescription(str);

    home.update(entry);
  }

  protected void deleteBean(int id)
  {
    if ( id != 0 )
    {
      home.remove(new BaseIntKey(id));
    }
  }

  protected void setBeanAttributes(HttpServletRequest req, int ID)
  {
    BoxResourceCatEntry entry = home.get(new BaseIntKey(ID));
    if (entry == null)
    {
      setDefaultAttributes(req);
      return;
    }
    //Set the defalut attributes for a new entry
    req.setAttribute("ID", new Integer(entry.getID()).toString());
    req.setAttribute("NAME", entry.getName());
    req.setAttribute("DESCRIPTION", entry.getDescription());
  }

  protected void setDefaultAttributes(HttpServletRequest req)
  {
    //Set the defalut attributes for a new entry
    req.setAttribute("ID", "0");
    req.setAttribute("NAME", "");
    req.setAttribute("DESCRIPTION", "");
  }

  public void setViewJSP(String str)
  {
    viewJSP = str;
  }
  public void setEditJSP(String str)
  {
    editJSP = str;
  }
  public void setListJSP(String str)
  {
    listJSP = str;
  }
}